#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QWidget>
#include<QPushButton>
#include<QString>
#include<QMouseEvent>
#include<QLabel>
#include<QResizeEvent>
#include<QHBoxLayout>
#include<QTimer>
#include<QWidget>
#include<QPixmap>
#include<QPainter>
#include"candy.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowIcon(QPixmap(":/res/Ramuda.png"));
    this->setWindowTitle("Amemura's Ame");
    this->setFixedSize(900,800);

    gameWidget = new QWidget(this);
    gameWidget->move(0, 100);
    gameWidget->resize(900, 700);
    gameWidget->setStyleSheet("background-color:white;");

    rmd = new QLabel(gameWidget);
    rmd->setGeometry(0, 300, 100, 50);
    rmd->setStyleSheet("border-image: url(:/res/Ramuda.png);");

    a = (0 + rand() % 9000)/double(10);
    b = 0 + rand() % 150;
    Candy drops;

    QString button_style="QPushButton{background-color:rgb(152,188,228);\
    color: black;border-radius:20px;border: 2px groove gray;\
    border-style: outset;}"
    "QPushButton:hover{background-color:white; color: black;}"
    "QPushButton:pressed{background-color:#4682B4;\
    border-style: inset; }";

     startBtn = new QPushButton(this);
     startBtn->setMaximumWidth(120);
     startBtn->setText("开始");
     startBtn->setStyleSheet(button_style);
     startBtn->setFont(QFont("黑体", 18));

     acBtn=new QPushButton(this);
     acBtn->setMaximumWidth(120);
     acBtn->setText("加速");
     acBtn->setStyleSheet(button_style);
     acBtn->setFont(QFont("黑体", 18));

     dcBtn = new QPushButton(this);
     dcBtn->setMaximumWidth(120);
     dcBtn->setText("减速");
     dcBtn->setStyleSheet(button_style);
     dcBtn->setFont(QFont("黑体", 18));

     stopBtn=new QPushButton(this);
     stopBtn->setMaximumWidth(120);
     stopBtn->setText("停止");
     stopBtn->setStyleSheet(button_style);
     stopBtn->setFont(QFont("黑体", 18));

     scorelb=new QLabel(this);
     scorelb->setGeometry(740,35,70,30);
     scorelb->setText("得分:");
     scorelb->setFont(QFont("黑体",18));

     score=new QLabel(this);
     score->setGeometry(820,35,50,30);
     score->setText("0");
     score->setFont(QFont("黑体",18));

     QWidget *layoutwindow = new QWidget(this);
     layoutwindow->setGeometry(20,10,700,80);
     QHBoxLayout *layout= new QHBoxLayout;
     layout->addWidget(startBtn);
     layout->addWidget(acBtn);
     layout->addWidget(dcBtn);
     layout->addWidget(stopBtn);
     layoutwindow->setLayout(layout);

     timer=new QTimer();
     //打开鼠标位置追踪
     this->setMouseTracking(true);
     gameWidget->setMouseTracking(true);
     gameWidget->installEventFilter(this);

     connect(startBtn, &QPushButton::clicked, this, &MainWindow::startgame);
     connect(stopBtn, &QPushButton::clicked, this, &MainWindow::stopgame);
     connect(acBtn,&QPushButton::clicked,this,&MainWindow::acgame);
     connect(dcBtn, &QPushButton::clicked, this, &MainWindow::dcgame);
     connect(timer, SIGNAL(timeout()), this, SLOT(rmdmove()));

     iniscore=0;
     delay=10;
     db=2;
     }
     //固定rmd纵向位置
     void MainWindow::resizeEvent(QResizeEvent * dragwindow)
     {
         if (dragwindow) {
             rmd->move(rmd->x(), gameWidget->height() - 50);
         }
     }

     void MainWindow::startgame()
     {
         timer->start();

     }

     void MainWindow::acgame()
     {
         delay = delay - 2;
         timer->setInterval(delay);
         timer->start();
     }

     void MainWindow::dcgame()
     {
         delay = delay + 2;
         timer->setInterval(delay);
         timer->start();
     }

     void MainWindow::stopgame()
     {
         timer->stop();
     }

     //碰撞检测
     void MainWindow::rmdmove()
     {
         if (drops.m_x>rmd->pos().x()-drops.m_tang.width() && drops.m_x<rmd->pos().x()+rmd->width())
              && (drops.m_y+drops.m_tang.height()>rmd->pos().y())
         {

             iniscore++;
             score->setText(QString("%1").arg(iniscore));
             //随机位置，保留1位小数
             a = (0 + rand() % 9000)/double(10);
             b = 0 + rand() % 150;
             drops.m_x=a;
             drops.m_y=b;
             Candy.updatePosition();
         }
         else if (drops.m_y>gameWidget->height())
         {
             a = (0 + rand() % 9000)/double(10);
             b = 0 + rand() % 150;
             drops.m_x=a;
             drops.m_y=b;
             Candy.updatePosition();
         }

         b += db;
         Candy.updatePosition();
     }

     //鼠标移动事件
     void MainWindow::mouseMoveEvent(QMouseEvent *event)
     {
         event->accept();
         if (event->pos().x()>0&& event->pos().x()<800)
         {
             rmd->move(event->pos().x(), rmd->pos().y());
         }
         else if (event->pos().x() >= 850)
         {
             rmd->move(800, rmd->pos().y());
         }
     }
     void MainWindow::paintEvent(QPaintEvent *event)
     {
         QPainter painter(this);
         painter.drawPixmap(drops.m_x,drops.m_y,drops.m_tang);
     }
     MainWindow::~MainWindow()
     {
         delete ui;
     }



